package com.doit.day03;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;

public class _02_练习题之生产者造数据 {
    public static void main(String[] args) throws InterruptedException {
        Properties props = new Properties();
        props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        props.setProperty(ProducerConfig.ACKS_CONFIG,"-1");
        KafkaProducer<String, String> producer = new KafkaProducer<String, String>(props);
        EventLog eventLog = new EventLog();

        while (true){
            eventLog.setGuid(RandomUtils.nextInt(10000,100000));
            eventLog.setEvent_id(RandomStringUtils.randomAlphabetic(10));
            eventLog.setTimestamp(System.currentTimeMillis());
            //fastjson 将对象转变成json串
            String json = JSON.toJSONString(eventLog);
            ProducerRecord<String, String> record = new ProducerRecord<>("event-log", json);
            producer.send(record);
            producer.flush();
            Thread.sleep(RandomUtils.nextInt(50,300));
        }

    }
}
